<?php


namespace App\Services;



use App\Models\ClassStudent;
use App\Models\Course;
use App\Models\CourseSectionWork;
use App\Models\School;
use App\Models\SchoolClassCourse;
use App\Models\Users;

class SchoolService extends BaseService
{
    /**
     * 根据名称获取名称
     * @param $name
     * @return User|null|Model
     */
    public function getByName($name){
        return School::query()->where('name',$name)->where('deleted_at',null)->first();
    }
    /**
     * 根据学校id获取学校信息
     * @param $id
     * @return User|null|Model
     */
    public function getSchoolInfo($id){
        return School::query()->where('id',$id)->where('deleted_at',null)->first();
    }
    /**
     * 根据学校id获取学生数量
     * @param $school_id
     * @return User|null|Model
     */
    public function getSchoolStudentNum($school_id){
        return Users::query()->where('school_id',$school_id)->where('deleted_at',null)
            ->where('type',0)
            ->distinct('id')->count();
    }
    /**
     * 获取列表
     * @return
     */
    public function listSchool($name, $page,$limit,$sort,$order) {
        $query = School::query();
        if(!empty($name)){
            $query->where('name','like',"%$name%");
        }
        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }

    /**
     * 学校课程
     * @return
     */
    public function schoolCourse($school_id,$coursesName) {
        $query = Course::leftJoin('school_courses', 'school_courses.course_id', '=', 'courses.id')
            ->select('courses.id','courses.name','courses.name','courses.pic','courses.pv','courses.abstract','school_courses.school_id');
        if(!empty($school_id)){
            $query->where('school_id',$school_id);
        }
        if(!empty($coursesName)){
            $query->where('courses.name','like',"%$coursesName%");
        }
        return $query->get();
    }
    /**
     * 用户是否有学校课程
     * @return
     */
    public function ispossessSchoolCourse($users_id,$course_id) {

        $classStudent = ClassStudent::where('users_id',$users_id)->first();
        if(!is_null($classStudent)){
            $schoolClassCourse = SchoolClassCourse::where('course_id',$course_id)
                ->where('school_id',$classStudent->school_id)
                ->where('class_grades_id',$classStudent->class_grades_id)
                ->first();
            if(!is_null($schoolClassCourse)){
                return 1;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }
    /**
     * 用户是否有添加课程作业
     * @return
     */
    public function isWorks($school_id,$course_id) {

        $courseSectionWork = CourseSectionWork::where('school_id',$school_id)->where('course_id',$course_id)->first();
        if(!is_null($courseSectionWork)){
           return 1;
        }else{
           return null;
        }
    }
    /**
     * 获取有作业的课程
     */
    public function worksCourse($courseArr,$school_id,$class_grades_id=null) {
        $query = CourseSectionWork::where('school_id',$school_id);
        if(!is_null($class_grades_id)){
            $query->where('class_grades_id',$class_grades_id);
        }
        $courseSectionWork = $query->whereIn('course_id',$courseArr)->get();
        return $courseSectionWork;
    }

}
